package
{
	import CCorrection.c_corObj;
	
	import UIFree.ObjectEvent;
	
	import UIUtils.uif_util;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.geom.Rectangle;
	import flash.system.Security;

	public class c_correction extends Sprite {
		public static var _app: Object;
		private var _dialog: Object;
		private var _cy: int;
		private var _sb_brihtness: Object;
		private var _sb_contrast: Object;
		private var _sb_red: Object;
		private var _sb_green: Object;
		private var _sb_blue: Object;
		
		private var _lb_brihtness: Object;
		private var _lb_contrast: Object;
		private var _lb_colors: Object;
		
		public function c_correction() {
			Security.allowDomain('*');
			super();
		}
		
		public function init(a_app: Object, extendsLayer: Object): void {
			_app = a_app;
			initStyles();
			_app.classes.cardbody.serviceClass['COR'] = c_corObj;
			_app.addEventListener('EXTENDRELEASE', doDialogShow);
//			_app.addEventListener('EDITORPHOTOLOADCOMPLETE', doPhotoLoad);
		}
		
/*		protected function doPhotoLoad(e: ObjectEvent): void {
			var image: Object = e.object;  
			var bmd: BitmapData = new BitmapData(image.contentLoaderInfo.width, image.contentLoaderInfo.width);
			bmd.draw(image as DisplayObject);
			var v: Vector.<Vector.<Number>> = bmd.histogram();
			v[0];
		}*/
		
		private function initStyles(): void {
			_app.styles['sbhRed'] = uif_util.union(_app.styles['scrollHandle'], {
				fill_color: 0xFF0000
			});			
			_app.styles['sbhGreen'] = uif_util.union(_app.styles['scrollHandle'], {
				fill_color: 0x00FF00
			});			
			_app.styles['sbhBlue'] = uif_util.union(_app.styles['scrollHandle'], {
				fill_color: 0x0000FF
			});			
			_app.styles['sbRed'] = uif_util.union(_app.styles['scrollbar'], {
				handleClass: 'sbhRed'
			});			
			_app.styles['sbGreen'] = uif_util.union(_app.styles['scrollbar'], {
				handleClass: 'sbhGreen'
			});			
			_app.styles['sbBlue'] = uif_util.union(_app.styles['scrollbar'], {
				handleClass: 'sbhBlue'
			});			
		}
		
		private function doFullViewChange(e: Event): void {
			if (_app.isFullView && _dialog.visible)
				_dialog.slideHide();
		}
		
		private function createDialog(parent: Object): void {
			var inner: Rectangle = parent.getBounds(_app);
			inner.inflate(-10, -10);
			_dialog = new _app.classes.window(_app.layers[2], inner.x, inner.y + 15, 230, 290, 'window', false, 1);
			_dialog.title.text = 'Коррекция';
			_dialog.visible = false;
			_cy = -30;
			_lb_brihtness 	= createLabel();
			_sb_brihtness 	= createScroll(updateMatrix);
			_lb_contrast 	= createLabel();
			_sb_contrast 	= createScroll(updateMatrix);
			_lb_colors 		= createLabel();
			_sb_red 		= createScroll(updateMatrix, 'sbRed');
			_sb_green 		= createScroll(updateMatrix, 'sbGreen');
			_sb_blue 		= createScroll(updateMatrix, 'sbBlue');
			inner 			= _dialog.getInner();
			var resetButton: Object = new _app.classes.button(_dialog, inner.x + (inner.width - 80) / 2, inner.y + _cy + 33, 80, 30, 'button');
			resetButton.text = 'Сбросить';
			resetButton.addEvent(MouseEvent.CLICK, reset);
			updateLabels();
			_app.addEventListener('FULLVIEWCHANGE', doFullViewChange);
			_app.addEventListener('COMPLETELOADCARD', doCompleteLoadCard);
		}
		
		protected function doCompleteLoadCard(e: ObjectEvent): void {
			if (e.object == _app.editor.cardBody) reset();
		}
		
		protected function updateMatrix(e: Event = null): void {
			updateLabels();
			var br: Number 		= _sb_brihtness.value / 100;
			var con: Number 	= (100 + _sb_contrast.value) / 100;
			var red: Number 	= 1 + _sb_red.value / 100;
			var green: Number 	= 1 + _sb_green.value / 100;
			var blue: Number 	= 1 + _sb_blue.value / 100;

			var colors: Object 		= new ColorTransform();
			colors.blueMultiplier 	= con * blue;
			colors.greenMultiplier 	= con * green;
			colors.redMultiplier 	= con * red;
			colors.blueOffset 		= 0x7F * (1 - con) + 0xFF * br;
			colors.greenOffset 		= 0x7F * (1 - con) + 0xFF * br;
			colors.redOffset 		= 0x7F * (1 - con) + 0xFF * br;
			doUpdateCardbody(colors);			
		}
		
		protected function doUpdateCardbody(colors: Object): void {
			var cbody: Object 		= _app.cardbody;
//			cbody.applyPhotoMatrix(cbody.photoMatrix);
			cbody.colorsExt = colors;
			
			if (!cbody.maskInfo.extend) cbody.maskInfo.extend = new Object();
			applyParams();
		}
		
		protected function applyParams(): void {
			var cbody: Object  = _app.cardbody;
			var params: String = '';
			if (cbody.hasOwnProperty('photos') && cbody.photos) {
				cbody.photos.forEach(function(item: Object, i: int, arr: Array): void {
					params += (params?',':'') + colorsPack(item.ct);
				})
			} else params = colorsPack(cbody.colorsExt);
			cbody.maskInfo.extend['COR'] = ['COR', params];
		}
		
		protected static function colorsPack(colors: Object): String {
			return colors.blueMultiplier + ',' + 
					colors.greenMultiplier + ',' + 
					colors.redMultiplier + ',' + 
					colors.blueOffset + ',' + 
					colors.greenOffset + ',' + 
					colors.redOffset;
		}
		
		protected function reset(e: Event = null): void {
			_sb_brihtness.value = 0;
			_sb_contrast.value  = 0;
			_sb_red.value  		= 0;
			_sb_green.value  	= 0;
			_sb_blue.value  	= 0;
			updateLabels();
		}
		
		protected function updateControls(): void {
			var colors: Object 		= _app.cardbody.maskInfo.colors;
			_sb_brihtness.value 	= (colors.blueOffset + colors.greenOffset + colors.redOffset) / (3 * 0xFF) * 100;
			_sb_contrast.value		= 0;
			_sb_red.value			= colors.redOffset / 0xFF * 100;
			_sb_green.value			= colors.greenOffset / 0xFF * 100;
			_sb_blue.value			= colors.blueOffset / 0xFF * 100;
			updateLabels();
		}
		
		protected function updateLabels(): void {
			_lb_brihtness.text 	= 'Яркость ' + _sb_brihtness.value.toString();
			_lb_contrast.text 	= 'Контрастность ' + _sb_contrast.value.toString();
			_lb_colors.text		= 'Цветовой баланс';
		} 
		
		protected function createScroll(onValue: Function, a_styleClass: String = 'scrollbar'): Object {
			var inner: Rectangle = _dialog.getInner();
			_cy += 33;
			return new _app.classes.scrollbar100(_dialog, inner.x, inner.y + _cy, inner.width, onValue, a_styleClass);			
		}
		
		protected function createLabel(caption: String=''): Object {
			var inner: Rectangle = _dialog.getInner();
			_cy += 25;
			var label: Object = new _app.classes.label(_dialog, inner.x, inner.y + _cy + 5, inner.width, 25, 'lbLabel');
			label.text = caption;
			return label;
		}
		
		protected function doDialogShow(e: ObjectEvent): void {
			if (!_dialog) createDialog(e.object.parent.parent);
			_dialog.slideShow();
		}
	}
}